home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 370 < prev    next >
Text File  |  1996-08-06  |  3KB  |  104 lines

  1. Path: news.iadfw.net!usenet
  2. From: arlyn@airmail.net (Arlyn Chesley)
  3. Newsgroups: comp.std.c
  4. Subject: Need Help with stacks - advanc_c.cpp (0/1)
  5. Date: Sat, 24 Feb 1996 09:32:21 GMT
  6. Organization: customer of Internet America
  7. Message-ID: <4gnt60$ois@news-f.iadfw.net>
  8. NNTP-Posting-Host: dal22-08.ppp.iadfw.net
  9. X-Newsreader: Forte Free Agent v0.55
  10.  
  11. I am having diffficulty getting info back from stacks.   If anyone out
  12. there can help it would be appreciated.
  13.  
  14. I have the main included as an attachment and will copy in my include
  15. file as I do not know if I can attach 2 files.  I am using a Borland
  16. C++ compiler but am programming in"C".
  17.  
  18. /**************************************************************************
  19. *    File Name:    p1.cpp
  20. *    Description:    Using Stacks
  21. *    Author:        Arlyn K. Chesley
  22. *    Date:          2/16/96 started
  23. *
  24. *       This file contains four functions that are used in conjunction
  25. *       with the main of stacks.cpp
  26. *       The four functins are as follows
  27. *       stack_new  - a function used to create new stacks
  28. *       stack_push - used to push items onto the stack
  29. *       stack_pop  - used to pop items off of the stack for use
  30. *       stack_free - used to free the memory previously allocated to
  31. stacks
  32. *       I = an Input to the function, O = an output from the function
  33. *************************************************************************/
  34. int stack_new(
  35.          int size,       /* I-size of a single element                */
  36.          int count,      /* I-Maximum number of data elements         */
  37.          long *k)        /* O-Key to the newly created stack area     */
  38.          {
  39.          /*************************************************************
  40.          *  Structure definition for allocation of new stacks
  41.          **************************************************************/
  42.         struct str_hdr
  43.           {
  44.           int tos;
  45.           int size;
  46.           int count;
  47.           char *data;
  48.           };
  49.  
  50.          struct str_hdr *H;
  51.          H=(str_hdr*)malloc(sizeof(str_hdr));
  52.          H->tos = 0;
  53.          H->size = size;
  54.          H->count = count;
  55.          H->data =(char*)malloc(sizeof(size * count));
  56.          *k =(long)H;     /* pointer back to main for key */
  57.  
  58.          if(H == NULL)
  59.         return(RC_MALLOC_ERROR);
  60.          else
  61.         return(RC_OK);
  62.          }
  63.  
  64. int stack_push(
  65.          long key,       /* I-Key to the stack to operate upon        */
  66.          char *data)     /* I-Pointer to data element to be saved     */
  67.          {
  68.          if(data != NULL)
  69.         {
  70.         (char)key = *data;
  71.         key++;
  72.         return RC_OK;
  73.         }
  74.          else
  75.         {
  76.         return RC_OVERFLOW;
  77.         }
  78.          }
  79.  
  80. int stack_pop(
  81.          long key,     /* I-Key to the stack to operate upon        */
  82.          char *data)     /* O-Place retrieved data is to be copied to */
  83.          {
  84.          if(data != NULL)
  85.           {
  86.           key = *data;
  87.           key--;
  88.          return(RC_OK);
  89.           }
  90.          else if(data == NULL)
  91.         return(RC_UNDERFLOW);
  92.          else
  93.         return(0);
  94.          }
  95.  
  96. int stack_free(
  97.          long key)       /* I-Key to the stack to operate upon        */
  98.          {
  99.          free((long *) key);
  100.          return(0);
  101.          }
  102.  
  103.  
  104.